﻿#region Using References

using System.IO;
using Beaker.Services.FileSystem;
using Newtonsoft.Json;

#endregion

namespace Beaker.OpenCube.IO
{
	public static class MetadataFileExtension
	{
		/// <summary>
		/// Read the content of the file as metadata.
		/// </summary>
		/// <typeparam name="TMetadata">The type of the target metadata entities.</typeparam>
		/// <param name="file">The file to read as metadata.</param>
		/// <returns>An instance of the metadata with the data from the file.</returns>
		public static TMetadata ReadMetadata<TMetadata>( this IFile file )
			where TMetadata : class
		{
			if( file == null )
				return null;

			using( TextReader reader = file.OpenText() )
			{
				var serializer = new JsonSerializer();
				return serializer.Deserialize<TMetadata>( new JsonTextReader( reader ) );
			}
		}
	}
}
